package com.example.androidframework.ui.login

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.example.androidframework.R
import com.example.androidframework.databinding.ActivityLoginBinding
import com.example.androidframework.ui.main.MainActivity
import com.lxj.xpopup.XPopup
import com.martin.lib_base.LibBase
import com.martin.lib_base.base.BaseActivity
import com.martin.lib_base.ktx.normalToast
import com.martin.lib_base.utils.FishUtil
import com.xuexiang.xui.utils.StatusBarUtils
import dev.utils.app.SpanUtils

class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {

    override fun getViewModelClass(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun beforeSetContentView() {
        super.beforeSetContentView()
        StatusBarUtils.translucent(this)
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        binding.viewModel = viewModel

        // 设置用户协议文本
        setUserAgreement()
        // 监听登录状态的改变
        observeLoginStatus()

        binding.agreeCheckbox.setOnClickListener {
            viewModel.agree.value = binding.agreeCheckbox.isChecked
        }

        binding.navBack.setOnClickListener {
            viewModel.goBack()
        }


        binding.loginBtn.setOnClickListener {
            if (FishUtil.isClickable()) {
                viewModel.login()
            } else {
                normalToast("不可连续点击")
            }
        }
    }

    /// 设置用户协议富文本
    private fun setUserAgreement() {
        val userAgreement = SpannableString("《用户协议》")
        userAgreement.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 用户协议
                XPopup.Builder(this@LoginActivity).asCustom(
                    UserPrivatePopupView(this@LoginActivity, 1, onCancel = {
                        viewModel.agree.value = false
                    }, onConfirm = {
                        viewModel.agree.value = true
                    })
                ).show()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                // 设置文字颜色
                ds.color = Color.parseColor("#EF8201")
                // 设置下划线
                ds.isUnderlineText = true
            }
        }, 0, userAgreement.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        val privateAgreement = SpannableString("《隐私政策》")
        privateAgreement.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 隐私协议
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.parseColor("#EF8201")
                ds.isUnderlineText = true
            }
        }, 0, privateAgreement.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        val sp = SpanUtils.with(binding.userAgreement).append("登录代表你已经同意").append(userAgreement).append("及").append(privateAgreement).create()

        binding.userAgreement.apply {
            movementMethod = LinkMovementMethod.getInstance()
            highlightColor = Color.TRANSPARENT
            text = sp
        }
    }

    /// 监听登录状态的改变
    private fun observeLoginStatus() {
        LibBase.isLogin.observe(this) {
            if (it) {
                MainActivity.start(this)
                finish()
            }
        }
    }

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, LoginActivity::class.java)
            context.startActivity(intent)
        }
    }


}